<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>

</body>
<script>
  function arr(s) {
    let count = 0;
    // 反向遍历给定字符串
    for (let i = s.length - 1; i >= 0; i--) {
      // 当字符不等于空格时，此时为字母，count增加1
      if (s[i] != ' ') count++;
      else if (count != 0) return count;
    }
    // 返回0(为了解决当给定字符串全为空格时的情况)
    return 0;
  }
  console.log(arr(s = "Hello World"));
</script>--·

</html>